let currentQuestion = 0;
const answers = {};
const totalQuestions = document.querySelectorAll('.question-card').length;

function saveAnswer(input) {
    const questionId = input.closest('.question-card').dataset.questionId;
    answers[questionId] = input.value.trim();
}

function nextQuestion() {
    const questions = document.querySelectorAll('.question-card');
    
    // 检查是否已输入答案
    const currentQuestionId = questions[currentQuestion].dataset.questionId;
    if (!answers[currentQuestionId]) {
        alert('请输入你的答案');
        return;
    }
    
    // 隐藏当前问题，显示下一问题
    questions[currentQuestion].classList.add('hidden');
    currentQuestion++;
    
    if (currentQuestion < questions.length) {
        questions[currentQuestion].classList.remove('hidden');
        document.getElementById('progress').textContent = 
            `问题 ${currentQuestion + 1} / ${totalQuestions}`;
    }
    
    // 最后一个问题时显示提交按钮
    if (currentQuestion === questions.length - 1) {
        document.getElementById('next-btn').classList.add('hidden');
        document.getElementById('submit-btn').classList.remove('hidden');
    }
}

async function submitAnswers() {
    try {
        const response = await fetch('/submit', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(answers)
        });
        
        const result = await response.json();
        window.location.href = `/result/${result.result_id}`;
    } catch (error) {
        console.error('提交答案时出错:', error);
        alert('提交答案时出错，请重试');
    }
}

function shareResult() {
    // 实现分享功能
    if (navigator.share) {
        navigator.share({
            title: '好友默契度测试结果',
            text: `我在好友默契度测试中获得了 ${document.querySelector('.text-6xl').textContent} 的成绩！来测测看我们有多默契吧！`,
            url: window.location.href
        }).catch(console.error);
    } else {
        alert('复制链接分享给好友吧！');
    }
} 